<template>
	<view class="main">
		<u-navbar
			:safeAreaInsetTop="false"
			title=""
			leftIconColor="#fff"
			bgColor="#043237"
			:fixed="true"
			:autoBack="true"
			leftText="猫币充值"
		>
		</u-navbar>
		<view class="main_recharge_head">
			<view class="head_box">
				<view class="head_row">
					猫币余额：{{info.money || 0}}
					<view class="chongzhi" @click="RechargeModal">充值</view>
				</view>
				<view class="head_row" style="font-size: 32rpx;">说明：1猫币=1元</view>
			</view>
			<u-tabs 
				:list="tabslist" 
				@click="clickType"
				:activeStyle="{
						color: '#fff',
						fontWeight: 'bold'
				}"
				:inactiveStyle="{
						color: '#fff',
						transform: 'scale(1)'
				}"
			></u-tabs>
		</view>
		<view class="main_recharge_content">
			<veiw  v-if="tableactive < 2">
				<block v-for="(item,index) in list" v-if="list.length > 0">
					<view class="company_box flex-wrap">
						<view class="title">{{item.describe || ""}}</view>
						<view style="display: flex;justify-content: space-between;width: 100%;">
							<view class="time">{{form.pm == 1 ? '充值时间：' : '操作时间'}}{{item.create_time || 0}}</view>
							<view style="color: firebrick;font-weight: bold;">￥{{item.money || 0}}</view>
						</view>
					</view>
				</block>
				<view class="loadingText">{{loadingText}}</view>
			</veiw>
			<view  v-else-if="tableactive == 2">
				<block v-for="(item,index) in entlist">
					<view class="qiyebox" @click="goto('/resume/entdetails?id='+item.ent_id+'&source=1')">
						<view style="flex: 1;width: 80rpx;height: 80rpx;margin-right:10rpx">
							<image style="max-width:100%;max-height:100%" src="../../static/img/qiye.jpg"></image>
						</view>
						<view class="company_item_box">
							<view class="company_name spill">
								{{item.ent_name || ""}}
							</view>
							<view class="company_addr">
								{{item.fieldname || ""}}
							</view>
						</view>
						<view style="display: flex;color:#ff6d6d;flex: 1;align-items: center;">	
							<image style="max-width:50rpx;max-height:50rpx;" src="../../static/img/hot.png"></image>
							{{item.view || 10}}
						</view>
					</view>
				</block>
			</view>
			<view class="main_recharge_foot" v-else-if="tableactive == 3">
				<text style="color: red;">猫币消费说明</text>
				<text>1、查看简历10猫币; </text>
				<text>2、查看视频;5猫币</text>
				<text>3、邀约人才5猫币(人才确定后扣取) </text>
			</view>
		</view>
		<u-modal
			title="猫币充值"
			:show="showModal"
			showCancelButton
			asyncClose
			@confirm="Recharge"
			@cancel="() => showModal = false"
		>
			<veiw class="content_box">
				<u--input
					placeholder="充值金额"
					prefixIcon="rmb"
					v-model="money"
					color="#ee0101"
					fontSize="48rpx"
					inputAlign="left"
					prefixIconStyle="font-size: 46rpx;font-weight: bold;color: #000"
				></u--input>
			</veiw>
		</u-modal>
		<Tabbar :pagePath="100"></Tabbar>
	</view>
</template>

<script>
	import Tabbar from "@/components/entTabbar"
	import {getMoneyAccount,getUserInfo,Recharge,RechargeSuccess} from "@/api/user"
	import { getKingEntList } from '@/api/ranking.js'
	import { imgURL } from '@/utils/config'
	export default {
		components: {
			Tabbar,
		},
		data() {
			return {
				showModal:false,
				tableactive:0,
				info:{},
				money: 0.00,
				entlist:[],
				imgURL:imgURL,
				tabslist: [{
						name: '充值记录',
				}, {
						name: '消费记录',
				}, {
						name: '企业榜'
				}, {
						name: '猫币使用说明'
				}],
				form:{
					page:1,
					pageSize:10,
					total:0,
					keyword:'',
					pm:1,
				},
				list:[],
				loadingText: '上拉显示更多'
			}
		},
		onLoad() {
			this.getUserInfo()
			this.account(1)
		},
		onPullDownRefresh() {
			this.form.page = 1
			this.account(this.form.pm)
		},
		onReachBottom() {
			this.getMoreList();
		},

		methods:{
			getUserInfo(){
				getUserInfo({}).then(res=>{
					if(res.code == 200){
						this.info = res.data
					}
					else{
						return uni.showToast({
							icon: 'none',
							title: res.msg
						});
					}
				})
			},
			clickType(e){
				this.tableactive = e.index
				switch(e.index){
					case 0:
						this.list = []
						this.account(1)
						break;
					case 1:
						this.list = []
						this.account(2)
						break;
					case 2:
						this.getEntList()
						break;
					default:
						
						break;
				}
			},
			account(pm){
				this.form.pm = pm
				getMoneyAccount(this.form).then(res=>{
					if(res.code == 200){
						this.list = res.data.data
						this.form.total = res.data.total
						this.form.page ++
					}
				})
			},
			
			getMoreList() {
				this.loadingText = '-- 正在加载 --'
				getMoneyAccount(this.form).then(res=>{
					if (res.code == 200) {
						if (res.data.data.length == 0 || res.data.data == null) {
							this.loadingText = '-- 暂无更多 --'
							return false;
						}
						var newlist = res.data.data;
						this.list = this.list.concat(newlist)
						this.form.page ++
					}
				});
			},

			
			RechargeModal(){
				this.showModal = true
			},
			Recharge(){
				let that = this
				// saveReserve 为业务接口
				Recharge({money:this.money}).then(res => {
					console.log("业务接口返回",res)
						// res为调起微信支付所需参数
						if(res.code == 200) {
							// 调起微信支付
							let data = {money:that.money,order_sn:res.data.order_sn}
							uni.requestPayment({
								provider: 'wxpay', // 服务提提供商
								timeStamp: res.data.timeStamp, // 时间戳
								nonceStr: res.data.nonceStr, // 随机字符串
								package: res.data.package,
								signType: res.data.signType, // 签名算法
								paySign: res.data.paySign, // 签名
								success: function (result) {
									that.showModal = false
									if(result.errMsg == "requestPayment:ok"){
										console.log("微信支付成功",result)
										RechargeSuccess(data).then(r=>{
											if(r.code == 200){
												uni.showToast({
													title:r.msg,
													duration: 2000,
													icon:'none',
													success() {
														that.getUserInfo()
													}
												});
											}
										})
									}
									// 业务逻辑。。。
								},
								fail: function (err) {
									console.log('支付失败',err);
								}
							});		
						}
					}).catch(err => {
						console.log(err);
					})
			},
			
			goto(tourl) {
				uni.navigateTo({
					url: tourl
				});
			},
			
			//获取企业信息
			getEntList() {
				getKingEntList({}).then(res=>{
					if(res.code == 200){
						this.entlist = res.data
					}
					else{
						return uni.showToast({
							icon: 'none',
							title: res.msg
						});
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.main{
		.u-navbar__content{
			opacity: 0.1 !important;
			background:none !important;
		}
		.main_recharge_head{
			background-image: url('');
			background-repeat:no-repeat;
			background-attachment:fixed;
			background-size:cover;
			width: 750rpx;
			padding-top: 50rpx;
			margin-top: 70rpx;
			.head_box{
				display: flex;
				flex-wrap: wrap;
				justify-content: center;
				color: #fff;
				.head_row{
					display: flex;
					flex-wrap: wrap;
					justify-content: center;
					text-align: center;
					width: 100%;
					font-size: 42rpx;
					height: 100rpx;
					line-height: 100rpx;
					align-items: center;
					.chongzhi{
						font-size: 26rpx;
						background-color: #989dfd;
						color: #fff;
						height: 40rpx;
						line-height: 40rpx;
						padding: 6rpx 20rpx;
						margin-left: 20rpx;
						border-radius: 6rpx;
					}
				}
			}
			image{
				position: fixed;
				top: 0;
				left: 0;
				width: 100%;
				max-height: 100%;
			}
		}
		.main_recharge_content{
			padding: 80rpx 40rpx;
			.flex-wrap{
				flex-wrap: wrap;  
			}
			.loadingText {
				padding: 20rpx 0 40rpx 0;
				text-align: center;
				color: #310D05;
				font-size: 28rpx;
			}
			.company_box{
				display: flex;  
				width: 100%;
				padding: 20rpx 0;
				border-bottom: 1px solid #ccc;
				font-size: 28rpx;
				.company_logo{
					display: flex;
					justify-content: center;
					align-items: center;
					width:100rpx; 
					height:100rpx;
					image{
						max-width: 100% !important;
						max-height: 100% !important;
					}
				}
				
				//文字溢出...
				.spill {
					white-space:nowrap;
					overflow:hidden;
					text-overflow:ellipsis;
				}
				.company_name{
					font-size: 34rpx;
					height: 60rpx;
					line-height: 60rpx;
				}
				.company_handle{
					margin: 20rpx;
					.handle_shenqin{
						width: 200rpx;
						height: 40rpx;
					}
				}
				.company_intro{
					line-height: 50rpx;
					color: #777777;
					margin-bottom: 20rpx;
				}
				.title{
					display: flex;
					flex-wrap: wrap;    
					justify-content: space-between;
					width: 100%;
					font-weight: bold;
					color: blue;
					line-height: 50rpx;
				}
				.time{
					color: #666;
				}
			}
			.qiyebox{
					display: flex;
					align-items: center;
					background-color: #f9f9ff;
					padding: 20rpx;
					font-size: 28rpx;
					color: #000;
					border-radius: 10rpx;
					margin-bottom: 20rpx;
					.company_name{
						width:460rpx;
						font-size: 30rpx;
						white-space:nowrap;
						overflow:hidden;
						text-overflow:ellipsis;
					}
					.company_handle{
						margin: 20rpx;
						.handle_shenqin{
							width: 200rpx;
							height: 80rpx;
						}
					}
					.company_intro{
						line-height: 50rpx;
						color: #777777;
						margin-bottom: 20rpx;
					}
					.company_addr{
						display: flex;
						align-items: center;
						color: #777777;
						font-size: 28rpx;
					}
			}
			.content_box{
				display: flex;
				flex-wrap: wrap;
				justify-content: space-between;
				.title{
					width: 100%;
					font-weight: bold;
					font-size: 26rpx;
					margin-bottom: 50rpx;
				}
			}
		
			.main_recharge_foot{
				background-color: #f6f4fe;
				border-radius: 20rpx;
				display: flex;
				flex-wrap: wrap;
				justify-content: center;
				margin: 50rpx 0rpx;
				padding:80rpx 70rpx;
				text{
					width: 100%;
					height: 60rpx;
					line-height: 60rpx;
					color: #222222;
				}
				image{
					width: 700rpx;
					max-height: 340rpx;
				}
			}
		}
	}
</style>